#include "Cclient.h"
CClient::CClient()
{
	/*连接服务器*/
	m_clientManager = std::make_shared<httplib::Client>("http://localhost:1234");
	if (m_clientManager)
	{
	std::cout<<"连接服务端成功"<<std::endl;
	m_clientFlag = 1;
	}
	else
	{
		std::cout<<"连接服务端失败"<<std::endl;
		m_clientFlag = 0;
        }
	m_clientHeader = {
		{"content-type","application/json"}
	};
}

void CClient::DoClient()
{
	/*执行客户端流程*/
	while(1)
	{
		int iKey;
		std::cout<<"按下8查询数据库，按下9插入数据,按下7删除数据,按下6更新数据"<<std::endl;
		std::cin>>iKey;
		if (iKey == 8)			//查询数据库
		{
			GetServer();
		}
		else if (iKey == 9)		//往数据库插入数据
		{
			ShowInput(9);
			SendPostToServer();
		}
		else if (iKey == 7)  		//删除数据库中指定数据
		{
			std::cout<<"用NULL代替空字符串"<<std::endl;
			ShowInput(7);
			DeleteServer();
		}
		else if (iKey == 6)		//更新数据库中指定数据
		{
			std::cout<<"输入要修改的id和name，id不能修改,NULL代替空字符串"<<std::endl;
			ShowInput(6);
			UpdateServer();
		}
		if (!IsContinue())
		{
			break;
		}
	}
}

void CClient::ShowInput(int type)
{
	/*提示用户交互*/
	std::string strType;
	if (type == 7)			
	{
		strType = "删除";
	}
	else if (type == 9)
	{
		strType = "插入";
	}
	else if (type == 6)
	{
		strType = "更新";
	}
	std::cout<<"请输入"<<strType<<"的id"<<std::endl;
	std::cin>>m_strId;
	std::cout<<"请输入"<<strType<<"的name"<<std::endl;
	std::cin>>m_strName;
	std::cout<<std::endl;
}

void CClient::SendPostToServer()
{
	/*发送增加请求到服务端*/
	m_clientJson["id"] = m_strId;		//初始化json字串
	m_clientJson["name"] = m_strName;
	std::string strValue = m_clientJson.dump();	//将json格式转化为string类型
	std::cout<<strValue<<std::endl;
	auto res = m_clientManager->Post("/post",m_clientHeader,strValue,"application/json"); 		//向服务端发送请求
	std::cout<<"status:"<<res->status<<std::endl;		//服务端向客户端返回状态
	std::cout<<res->body<<std::endl;			//输出服务端返回的正文
}

void CClient::GetServer()
{
	/*向服务端发送get请求*/
    std::cout<<"enter me"<<std::endl;
    auto resget = m_clientManager->Get("/mysqlclient", m_clientHeader);		//发送请求

    std::cout << "get:" << resget->body << std::endl;				//输出服务端返回的正文
}

void CClient::DeleteServer()
{
	/*发送delete请求到服务端*/
	m_clientJson["id"] = m_strId;		//初始化json字符串
	m_clientJson["name"]=m_strName;
	std::string strDelete = m_clientJson.dump();	//将json格式转化为string类型
	std::cout<<strDelete<<std::endl;
	auto res = m_clientManager->Delete("/delete",m_clientHeader,strDelete,"application/json");	//发送请求
	std::cout<<"status:"<<res->status<<std::endl;	//输出服务端返回的状态
	std::cout<<res->body<<std::endl;		//输出服务端返回的正文
}

void CClient::UpdateServer()
{
	/*向服务端发送更新数据的请求*/
	m_clientJson["id"] = m_strId;
	m_clientJson["name"] = m_strName;
	std::string strUpdate = m_clientJson.dump();
	std::cout<<strUpdate<<std::endl;
	auto res = m_clientManager->Put("/update",m_clientHeader,strUpdate,"application/json");
	std::cout<<"status:"<<res->status<<std::endl;
	std::cout<<res->body<<std::endl;
}
//bool CClient::IsConnect()
//{
//	if(m_clientFlag==1)
//	{
//		return true;
//	}
//	else
//	{
//		return false;
//	}
//}
bool CClient::IsContinue()
{
	/*判断是否继续*/
	int iCont;
	std::cout<<"按下0退出，否则继续(1)"<<std::endl;
	std::cin>>iCont;
	if(!iCont)
	{
		return false;
	}
	else
	{
		std::cout<<"继续插入"<<std::endl;
		return true;
	}
}
